package com.woniu.config;

import com.woniu.utils.UserToken;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
import org.apache.shiro.realm.Realm;

import java.util.ArrayList;
import java.util.Collection;

public class MyModularRealmAuthenticator extends ModularRealmAuthenticator {

    @Override
    protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken) throws AuthenticationException {
        this.assertRealmsConfigured();
        Collection<Realm> realms=this.getRealms();

        UserToken token= (UserToken) authenticationToken;
        String loginType= token.getLoginType();
        System.out.println("loginType--------------------"+loginType);

        Collection<Realm> typeRealms=new ArrayList<>();
        for (Realm realm : realms) {
            if (realm.getName().startsWith(loginType)){
                System.out.println("realm.getName()--------------------"+realm.getName());
                typeRealms.add(realm);
            }
        }
        if (typeRealms.size()==1){
            return this.doSingleRealmAuthentication(typeRealms.iterator().next(), authenticationToken);
        }else{
            return this.doMultiRealmAuthentication(typeRealms,authenticationToken);
        }
    }

}
